import number from "../components/lin-ui/common/async-validator/validator/number";
import {
  rpxToPx
} from "../utils/util"
Page({


  data: {
    flag: true,
    page: 1,
    list: [],
    btnH: 0,

    money: 0,
    num: 0,
    all_check: false,
  },


  onLoad(options) {
    wx.removeStorageSync('invoice_check')
    this.ReleaseWorkLlists();
    if (getApp().globalData.isIphoneX) {
      this.setData({
        btnH: rpxToPx(132 + 68)
      })
    } else {
      this.setData({
        btnH: rpxToPx(132)
      })
    }
  },

  onReachBottom() {
    this.ReleaseWorkLlists()
  },

  change_check(e) {
    let type = e.currentTarget.dataset.type;
    let list = this.data.list;
    if (type == 'all') {
      var all_check = !this.data.all_check;
      list.forEach((v, i) => {
        v.check = all_check
      })
      this.setData({
        all_check,
        list
      })
    } else {
      let index = e.currentTarget.dataset.index
      let val = `list[${index}].check`
      this.setData({
        [val]: !this.data.list[index].check
      })
      let is_check = true;
      this.data.list.forEach((v, i) => {
        is_check = is_check && v.check
      })
      this.setData({
        all_check: is_check
      })
    }
    this.calculation()
  },

  calculation() {
    let list = this.data.list;
    let num = 0;
    let money = 0;
    let arr = []
    list.forEach((v, i) => {
      if (v.check) {
        num += 1
        money += Number(v.money);
        arr.push(v)
      }
    })
    this.setData({
      num,
      money
    })
    wx.setStorageSync('invoice_check', {
      list: arr,
      money
    })
  },

  to_detail() {
    if (!wx.getStorageSync('invoice_check') || wx.getStorageSync('invoice_check').list.length == 0) {
      return
    }
    wx.navigateTo({
      url: './detail',
    })
  },

  ReleaseWorkLlists() {
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: true
    })
    getApp().globalData.post({
      data: {
        do: 'ReleaseWorkLlists',
        page: this.data.page,
        pagesize: 10,
        uid: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1
        })
      }
    })
  },

})